import { _decorator, Component, Label, Node } from 'cc';
import { ModuleBase } from './ModuleBase';
import { scoreData } from './Configs';
const { ccclass, property } = _decorator;

@ccclass('Score')
export class Score extends ModuleBase {
    private _curScoreLabel: Label = null;
    private _bestScoreLabel: Label = null;
    private _curScore: number = 0;
    private _bestScore: number = 0;
    onInit(...rest: any[]): void {
        this._bestScoreLabel = this.node.getChildByName("BestScore").getComponent(Label);
        this._curScoreLabel = this.node.getChildByName("CurScore").getComponent(Label);
        this._bestScore = (<number><unknown>localStorage.getItem("Cannon_BestScore")) == undefined ? 0 : (<number><unknown>localStorage.getItem("Cannon_BestScore"));
        this._curScore = 0;
        scoreData.bestScore = this._bestScore;
        scoreData.curScore = this._curScore;
        this._bestScoreLabel.string = `最高得分：${this._bestScore}`;
        this._curScoreLabel.string = `当前得分：${this._curScore}`;
    }

    onUpdate(dt: number, ...rest: any[]): void {


    }
    updateScore() {
        this._curScore += 1;
        scoreData.curScore = this._curScore;
        this._curScoreLabel.string = `当前得分：${this._curScore}`;
        if (this._curScore > this._bestScore) {
            this._bestScore = this._curScore;
            this._bestScoreLabel.string = `最高得分：${this._bestScore}`;
            scoreData.bestScore = this._bestScore;
            localStorage.setItem("Cannon_BestScore", <string><unknown>this._bestScore);
        }
    }

}


